package com.mvvmhabit.org.base

import android.app.Application
import android.content.Intent
import android.os.Bundle
import androidx.databinding.ObservableInt
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.mvvmhabit.org.bus.event.SingleLiveEvent
import com.mvvmhabit.org.custom.LoadingLayout
import com.mvvmhabit.org.custom.load.LoadDialog
import java.lang.ref.WeakReference

open class BaseViewModel(application: Application) : AndroidViewModel(application), IBaseViewModel {
    //弱引用持有
    private var lifecycle: WeakReference<LifecycleOwner>? = null

    // 注册
    private var uc: UIChangeLiveEvent<Any>? = null

    /**
     * 注入RxLifecycle生命周期
     *
     * @param lifecycle
     */
    fun injectLifecycleProvider(lifecycle: LifecycleOwner) {
        this.lifecycle = WeakReference(lifecycle)
    }

    fun getLifecycleProvider(): LifecycleOwner {
        return lifecycle!!.get()!!
    }

    fun getUI(): UIChangeLiveEvent<Any> {
        if (uc == null) {
            uc = UIChangeLiveEvent()
        }
        return uc!!
    }

    /**
     * 关闭界面
     */
    fun finish() {
        uc?.getFinishEvent()?.call("")
    }

    /**
     * 跳转页面
     * @param clz    所跳转的目的Activity类
     * @param bundle 跳转所携带的信息
     */
    fun startActivity(clz: Class<*>, bundle: Bundle?) {
        val params: HashMap<String, Any> = HashMap()
        params[CLASS] = clz
        if (bundle != null) {
            params[BUNDLE] = bundle
        }
        uc?.getStartActivityEvent()?.postValue(params)
    }

    /**
     * 跳转页面
     * @param clz    所跳转的目的Activity类
     */
    fun startActivity(clz: Class<*>) {
        val params: HashMap<String, Any> = HashMap()
        params[CLASS] = clz
        uc?.getStartActivityEvent()?.postValue(params)
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     * @param bundle        跳转所携带的信息
     */
    fun startContainerActivity(canonicalName: String?, bundle: Bundle?) {
        val params: HashMap<String, Any> = HashMap()
        if (canonicalName != null) {
            params[CANONICAL_NAME] = canonicalName
        }
        if (bundle != null) {
            params[BUNDLE] = bundle
        }
        uc?.getStartContainerActivityEvent()?.postValue(params)
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     * @param bundle        跳转所携带的信息
     */
    fun startContainerActivity(canonicalName: String?) {
        val params: HashMap<String, Any> = HashMap()
        if (canonicalName != null) {
            params[CANONICAL_NAME] = canonicalName
        }
        uc?.getStartContainerActivityEvent()?.postValue(params)
    }

    /**
     * 开启分享
     */
    fun startActivity(intent: Intent) {
        uc?.getStartIntentEvent()?.postValue(intent)
    }

    fun showLoadDialog(title: String) {
        uc?.getShowLoadDialogEvent()?.postValue(title)
    }

    fun showLoadDialog() {
        uc?.getShowLoadDialogEvent()?.postValue("")
    }

    fun dismissLoadDialog() {
        uc?.getDismissLoadDialogEvent()?.postValue("")
    }

    class UIChangeLiveEvent<T> : SingleLiveEvent<T>() {
        // 关闭界面的事件回调
        private var finishEvent: SingleLiveEvent<String>? = null

        // 分享
        private var startIntentEvent: SingleLiveEvent<Intent>? = null

        // 界面调跳转
        private var startActivityEvent: SingleLiveEvent<Map<String, Any>>? = null

        // 跳转到公共Activity 里面
        private var startContainerActivityEvent: SingleLiveEvent<Map<String, Any>>? = null

        // 开启加载数据的Dialog
        private var showLoadDialogEvent: SingleLiveEvent<String>? = null

        // 关闭加载数据的Dialog
        private var dismissLoadDialogEvent: SingleLiveEvent<String>? = null

        // 获取 关闭界面的事件回调
        fun getFinishEvent(): SingleLiveEvent<String> {
            if (finishEvent == null) {
                finishEvent = SingleLiveEvent()
            }
            return finishEvent!!
        }

        fun getStartIntentEvent(): SingleLiveEvent<Intent> {
            if (startIntentEvent == null) {
                startIntentEvent = SingleLiveEvent()
            }
            return startIntentEvent!!
        }

        // 跳转页面
        fun getStartActivityEvent(): SingleLiveEvent<Map<String, Any>> {
            if (startActivityEvent == null) {
                startActivityEvent = SingleLiveEvent()
            }
            return startActivityEvent!!
        }

        // 跳转容器页面
        fun getStartContainerActivityEvent(): SingleLiveEvent<Map<String, Any>> {
            if (startContainerActivityEvent == null) {
                startContainerActivityEvent = SingleLiveEvent()
            }
            return startContainerActivityEvent!!
        }

        // 开启加载数据的Dialog
        fun getShowLoadDialogEvent(): SingleLiveEvent<String> {
            if (showLoadDialogEvent == null) {
                showLoadDialogEvent = SingleLiveEvent()
            }
            return showLoadDialogEvent!!

        }

        // 关闭加载数据的Dialog
        fun getDismissLoadDialogEvent(): SingleLiveEvent<String> {
            if (dismissLoadDialogEvent == null) {
                dismissLoadDialogEvent = SingleLiveEvent()
            }
            return dismissLoadDialogEvent!!
        }

        override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
            super.observe(owner, observer)
        }
    }

    override fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
    }

    override fun onCreate() {
    }

    override fun onDestroy() {
    }

    override fun onStart() {
    }

    override fun onStop() {
    }

    override fun onResume() {
    }

    override fun onPause() {
    }

    override fun registerBus() {
    }

    override fun removeBus() {
    }

    override fun onCleared() {
        super.onCleared()

    }
}